use eframe::egui::{self, Ui, Window, Id};
use crate::models::{ServerConfig, ServerType};

pub struct ServerDialog {
    pub server: ServerConfig,
    pub open: bool,
    pub is_edit_mode: bool,
    server_index: Option<usize>,
}

impl ServerDialog {
    pub fn new() -> Self {
        Self {
            server: ServerConfig::default(),
            open: false,
            is_edit_mode: false,
            server_index: None,
        }
    }

    pub fn open_add(&mut self) {
        self.server = ServerConfig::default();
        self.is_edit_mode = false;
        self.server_index = None;
        self.open = true;
    }

    pub fn open_edit(&mut self, server: ServerConfig, index: usize) {
        self.server = server;
        self.is_edit_mode = true;
        self.server_index = Some(index);
        self.open = true;
    }

    pub fn show(&mut self, ctx: &egui::Context) -> Option<(ServerConfig, Option<usize>)> {
        let mut result = None;
        
        if self.open {
            let title = if self.is_edit_mode { "编辑服务器" } else { "添加服务器" };
            
            Window::new(title)
                .id(Id::new("server_dialog"))
                .fixed_size([400.0, 380.0])
                .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
                .collapsible(false)
                .resizable(false)
                .show(ctx, |ui| {
                    result = self.ui_content(ui);
                });
        }
        
        result
    }
    
    fn ui_content(&mut self, ui: &mut Ui) -> Option<(ServerConfig, Option<usize>)> {
        let mut result = None;
        let mut submitted = false;
        
        ui.vertical(|ui| {
            ui.add_space(8.0);
            
            // 服务器名称
            ui.horizontal(|ui| {
                ui.label("名称:");
                ui.add_sized([280.0, 24.0], egui::TextEdit::singleline(&mut self.server.name));
            });
            
            ui.add_space(8.0);
            
            // 服务器地址
            ui.horizontal(|ui| {
                ui.label("地址:");
                ui.add_sized([280.0, 24.0], egui::TextEdit::singleline(&mut self.server.host));
            });
            
            ui.add_space(8.0);
            
            // 端口
            ui.horizontal(|ui| {
                ui.label("端口:");
                ui.add(egui::DragValue::new(&mut self.server.port).clamp_range(1..=65535));
            });
            
            ui.add_space(8.0);
            
            // 服务器类型
            ui.horizontal(|ui| {
                ui.label("类型:");
                
                ui.radio_value(&mut self.server.server_type, ServerType::Shadowsocks, "Shadowsocks");
                ui.radio_value(&mut self.server.server_type, ServerType::Vmess, "Vmess");
                ui.radio_value(&mut self.server.server_type, ServerType::Trojan, "Trojan");
            });
            
            ui.add_space(8.0);
            
            // 密码
            ui.horizontal(|ui| {
                ui.label("密码:");
                ui.add_sized([280.0, 24.0], egui::TextEdit::singleline(&mut self.server.password));
            });
            
            ui.add_space(8.0);
            
            // 加密方式
            ui.horizontal(|ui| {
                ui.label("加密方式:");
                ui.add_sized([220.0, 24.0], egui::TextEdit::singleline(&mut self.server.encryption));
            });
            
            ui.add_space(16.0);
            
            // 按钮区域
            ui.horizontal(|ui| {
                if ui.button("取消").clicked() {
                    self.open = false;
                }
                
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    let submit_text = if self.is_edit_mode { "保存" } else { "添加" };
                    if ui.button(submit_text).clicked() {
                        submitted = true;
                    }
                });
            });
        });
        
        if submitted {
            // 克隆服务器配置，并将其与索引一起返回
            let server_config = self.server.clone();
            result = Some((server_config, self.server_index));
            self.open = false;
        }
        
        result
    }
}

impl Default for ServerDialog {
    fn default() -> Self {
        Self::new()
    }
} 